package com.leil.project

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.jetbrains.compose.ui.tooling.preview.Preview

@Composable
fun TextFieldDemo() {
    var state by rememberSaveable { mutableStateOf("") }
    TextField(
        value = state,
        onValueChange = { state = it },
        singleLine = true,
        label = { Text("TextField") },
    )
}

@Composable
fun BasicTextFieldDemo() {
    var state by rememberSaveable { mutableStateOf("") }
    BasicTextField(
        modifier = Modifier.width(200.dp).height(40.dp).border(
            width = 1.dp,
            color = MaterialTheme.colorScheme.outline,
            shape = RoundedCornerShape(4.dp)
        ).padding(10.dp),
        value = state,
        onValueChange = { state = it },
        singleLine = true,
        textStyle = TextStyle.Default.copy(fontSize = 20.sp)
    )
}

@Composable
fun OutlinedTextFieldDemo() {
    var inputText by remember { mutableStateOf(TextFieldValue()) }
    OutlinedTextField(
        value = inputText,
        onValueChange = { inputText = it },
        label = { Text("OutlinedTextField") },
        placeholder = { Text("Placeholder") },
        leadingIcon = { Icons.Default.Search },
        trailingIcon = { Icons.Default.Delete },
        singleLine = true,
        maxLines = 1,
        minLines = 1,
        textStyle = MaterialTheme.typography.bodyLarge,
        shape = RoundedCornerShape(4.dp),
    )
}

@Composable
fun SearchTextFieldByBasicTextFieldDemo() {
    var inputText by remember { mutableStateOf(TextFieldValue()) }
    Box(modifier = Modifier
        .width(150.dp)
        .height(40.dp)
        .border(1.dp, MaterialTheme.colorScheme.outline)) {
        Row(
            modifier = Modifier.fillMaxSize(),
            horizontalArrangement = Arrangement.SpaceAround,
            verticalAlignment = Alignment.CenterVertically
            ) {
            Icon(
                Icons.Default.Search,
                contentDescription = "Search",
            )
            BasicTextField(
                modifier = Modifier.weight(1f)
                    .padding(start = 5.dp, end = 5.dp, top = 8.dp, bottom = 8.dp),
                value = inputText,
                onValueChange = { inputText = it },
                textStyle = MaterialTheme.typography.bodyLarge.copy(
                    fontSize = 16.sp
                ),
                singleLine = true,
            )
            Icon(
                Icons.Default.Close,
                contentDescription = "Delete",
            )
        }
    }
}

@Composable
fun SearchTextFieldByTextFieldDemo() {
    var inputText by remember { mutableStateOf(TextFieldValue()) }
    Box(modifier = Modifier
        .height(100.dp)
        .width(250.dp)
        .background(Color.Cyan)
        ) {
        TextField(
            value = inputText,
            onValueChange = {inputText = it},
            leadingIcon = {
                Icon(
                    Icons.Default.Search,
                    contentDescription = "Search"
                )
            },
            trailingIcon = {
                Icon(
                    Icons.Default.Close,
                    contentDescription = "Delete",
                    modifier = Modifier.clickable {
                        inputText = TextFieldValue("")
                    }
                )
            },
            textStyle = MaterialTheme.typography.titleLarge,
            singleLine = true
        )
    }
}

@Preview
@Composable
fun DemoPreview() {
    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.SpaceAround,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        TextFieldDemo()
        BasicTextFieldDemo()
        SearchTextFieldByBasicTextFieldDemo()
        SearchTextFieldByTextFieldDemo()
        OutlinedTextFieldDemo()
    }
}